<?php
class CategoriaController extends Zend_Controller_Action {
	public function init(){
		require_once APPLICATION_PATH . '/abstract/FmtUtil.php';
		FmtUtil::isAdminAuth();
	}
	
	public function indexAction(){
		require_once APPLICATION_PATH . '/models/Categoria.php';
		$obj = new Categoria();
		$lista = $obj->getAll();
		
		$this->view->lista = $lista;
	}
	
	public function adicionarAction(){
		require_once APPLICATION_PATH . '/forms/CategoriaForm.php';
		$form = new CategoriaForm();
		
		$this->view->form = $form;
		$this->view->pageAction = "Adicionar";
		
	}
		
	public function editarAction(){
		$get = $this->getRequest();
		$codigo = $get->codigo;
		require_once APPLICATION_PATH . '/models/Categoria.php';
		$obj = new Categoria();
		$categoria = $obj->getById($codigo);
		
		require_once APPLICATION_PATH . '/forms/CategoriaForm.php';
		$form = new CategoriaForm();
		$form->setDefaults($categoria);
		$this->view->form = $form;
		$this->view->pageAction = "Editar";
		$this->render('adicionar');
	}
	
	public function saveAction(){
		if($this->getRequest()->isPost()){
			require_once APPLICATION_PATH . '/forms/CategoriaForm.php';
			$form = new CategoriaForm();
			
			$post = $this->getRequest();
			if($form->isValid($post->getPost())){
				require_once APPLICATION_PATH . '/models/Categoria.php';
				$obj = new Categoria();
				$obj->save($form->getValues());
			}
		}
		return $this->_helper->redirector('index');
	}
	
	public function deleteAction(){
		$get = $this->getRequest();
		$codigo = $get->codigo;
		
		require_once APPLICATION_PATH . '/models/Categoria.php';
		$obj = new Categoria();
		$obj->delete("cd_categoria = " . $codigo);
		
		return $this->_helper->redirector('index');
	}
}
?>